package com.cmy.webCore.utils;

import cn.hutool.extra.spring.SpringUtil;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;

/**
 * @Author:Cmy
 * @Date:2022/11/22-11:40
 * @Description:获取i18n资源文件
 */
public class MessageUtil {
    /**
     * 根据消息键和参数 获取消息 委托给spring messageSource
     * @param code 消息键
     * @param args 参数
     * @return 获取国际化翻译值
     */
    public static String message(String code, Object... args) {
        String message = "";
        try {
            MessageSource messageSource = SpringUtil.getBean(MessageSource.class);
            message = messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
        } catch (NoSuchMessageException e) {
            //e.printStackTrace();
        }
        return message;
    }
}

    